package com.youmans.eduservice.controller;


import com.youmans.commonutils.R;
import com.youmans.eduservice.client.VodClient;
import com.youmans.eduservice.entity.EduVideo;
import com.youmans.eduservice.service.EduVideoService;
import com.youmans.servicebase.exceptionHandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author Youmans
 * @since 2022-05-07
 */
@RestController
@Api(tags = "小节控制器")
@RequestMapping("/eduservice/video")
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService ;

    @Autowired
    private VodClient vodClient ;

    @ApiOperation("添加小节")
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo) ;
        return R.ok() ;
    }
    @ApiOperation("修改小节")
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo) ;
        return R.ok() ;
    }

    @ApiOperation("查询小节")
    @GetMapping("getVideo/{id}")
    public R getVideo(@PathVariable String id){
        EduVideo video = eduVideoService.getById(id);
        return R.ok().data("video",video) ;
    }

    //删除小节的时候，删除视频
    @ApiOperation("删除小节")
    @DeleteMapping("{id}")
    public R delete(@PathVariable String id){
//        根据小节id获取视频id,调用方法实现视频删除
        String videoSourceId = eduVideoService.getById(id).getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){{
            R result = vodClient.removeAliVideo(videoSourceId);
            if(result.getCode() == 20001){
                throw new GuliException(20001,"删除视频失败，熔断器。。.");
            }
        }}
        boolean b = eduVideoService.removeById(id);
        return b?R.ok():R.error();
    }
}

